package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.IotMessageRpcService;
import com.hunttown.mes.rpc.domain.IotMessageDTO;
import com.hunttown.mes.rpc.domain.query.IotMessageDTOQuery;
import com.hunttown.mes.service.IotMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.IotMessageConverts.convert;

/**
 * created by wangjunfu on 2020-11-20 11:15:00
 */
@Service
public class IotMessageRpcServiceImpl implements IotMessageRpcService {

    private final Logger logger = LoggerFactory.getLogger(IotMessageRpcServiceImpl.class);

    private final IotMessageService iotMessageService;

    @Autowired
    public IotMessageRpcServiceImpl(IotMessageService iotMessageService) {
        this.iotMessageService = iotMessageService;
    }

    @Override
    public IotMessageDTO insert(IotMessageDTO iotMessageDTO) {
        try {
            return convert(iotMessageService.insert(convert(iotMessageDTO)));
        } catch (Exception e) {
            logger.error("IotMessage create failed.", e);
            throw new RuntimeException("IotMessage create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(IotMessageDTO iotMessageDTO) {
        try {
            return iotMessageService.updateInfoById(convert(iotMessageDTO));
        } catch (Exception e) {
            logger.error("IotMessage update by id failed.", e);
            throw new RuntimeException("IotMessage update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(IotMessageDTOQuery iotMessageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotMessageDTOQuery.getW_id() != null && iotMessageDTOQuery.getW_id() > 0) {
                query.put("w_id", iotMessageDTOQuery.getW_id());
            }

            if (iotMessageDTOQuery.getIds() != null && !iotMessageDTOQuery.getIds().equals("")) {
                query.put("ids", iotMessageDTOQuery.getIds());
            }

            if (iotMessageDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotMessageDTOQuery.getStopFlag());
            }

            if (iotMessageDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotMessageDTOQuery.getDeleteFlag());
            }

            return iotMessageService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("IotMessage update by query failed.", e);
            throw new RuntimeException("IotMessage update by query failed, Please order your code.", e);
        }
    }

    @Override
    public IotMessageDTO selectById(Integer id) {
        try {
            return convert(iotMessageService.selectById(id));
        } catch (Exception e) {
            logger.error("IotMessage select by id failed.", e);
            throw new RuntimeException("IotMessage select by id failed, Please order your code.", e);
        }
    }

    @Override
    public IotMessageDTO selectObjByQuery(IotMessageDTOQuery iotMessageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(iotMessageService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("IotMessage select obj by query failed.", e);
            throw new RuntimeException("IotMessage select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<IotMessageDTO> getListForPage(IotMessageDTOQuery iotMessageDTOQuery) {
        Query query = Query.page(iotMessageDTOQuery.getPn(), iotMessageDTOQuery.getPageSize());

        if (iotMessageDTOQuery.getId() != null && iotMessageDTOQuery.getId() > 0) {
            query.put("id", iotMessageDTOQuery.getId());
        }

        if (iotMessageDTOQuery.getIds() != null && !iotMessageDTOQuery.getIds().equals("")) {
            query.put("ids", iotMessageDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(iotMessageDTOQuery.getLikeName())) {
            query.put("likeName", iotMessageDTOQuery.getLikeName());
        }

        if (iotMessageDTOQuery.getMsType() != null) {
            query.put("msType", iotMessageDTOQuery.getMsType());
        }

        if (iotMessageDTOQuery.getReceiveId() != null) {
            query.put("receiveId", iotMessageDTOQuery.getReceiveId());
        }

        if (iotMessageDTOQuery.getIsRead() != null) {
            query.put("isRead", iotMessageDTOQuery.getIsRead());
        }

        if (StringUtils.isNotBlank(iotMessageDTOQuery.getShowDate())) {
            query.put("showDate", iotMessageDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(iotMessageDTOQuery.getStartDate())) {
            query.put("startDate", iotMessageDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(iotMessageDTOQuery.getEndDate())) {
            query.put("endDate", iotMessageDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(iotMessageDTOQuery.getOrderBy())) {
            query.put("orderBy", iotMessageDTOQuery.getOrderBy());
        }

        List<IotMessageDTO> list = convert(iotMessageService.selectByQuery(query));
        Page<IotMessageDTO> page = Page.from(iotMessageDTOQuery.getPn(), iotMessageDTOQuery.getPageSize(), list);
        page.setTotalItems(iotMessageService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return iotMessageService.deleteById(id);
        } catch (Exception e) {
            logger.error("IotMessage delete failed.", e);
            throw new RuntimeException("IotMessage delete failed, Please order your code.", e);
        }
    }
}